package ca.llsutherland.squash.domain.helper;

import ca.llsutherland.squash.domain.Clock;
import ca.llsutherland.squash.domain.Court;
import ca.llsutherland.squash.domain.CourtStyle;
import ca.llsutherland.squash.domain.Gym;
import ca.llsutherland.squash.exceptions.ValidationException;
import ca.llsutherland.squash.utils.ErrorConstants;

public class CourtsHelper {

    public static Court createUnpersistedCourt(String name, Clock dateCreated, Clock dateUpdated) {
        return new Court(name, dateCreated, dateUpdated);
    }

    public static Court createUnpersistedCourt(String name, Clock dateCreated, Clock dateUpdated, String location) {
        Court court = createUnpersistedCourt(name, dateCreated, dateUpdated);
        court.setLocation(location);
        return court;
    }
    
    public static Court createUnpersistedCourt(String name, Clock dateCreated, Clock dateUpdated, String location, String specialNotes) {
        Court court = createUnpersistedCourt(name, dateCreated, dateUpdated, location);
        court.setSpecialNotes(specialNotes);
        return court;
    }
    
    public static Court createUnpersistedCourt(String name, Clock dateCreated, Clock dateUpdated, String location, String specialNotes, Gym gym) {
        Court court = createUnpersistedCourt(name, dateCreated, dateUpdated, location, specialNotes);
        court.setGym(gym);
        return court;
    }
    
    public static Court createUnpersistedCourt(String name, Clock dateCreated, Clock dateUpdated, String location, String specialNotes, Gym gym, CourtStyle courtStyle) {
        Court court = createUnpersistedCourt(name, dateCreated, dateUpdated, location, specialNotes, gym);
        court.setCourtStyle(courtStyle);
        return court;
    }

    public static Court createNewCourt(String name, Clock dateCreated, Clock dateUpdated, String location, String specialNotes, Gym gym, CourtStyle courtStyle, Long id) {
        Court court = createUnpersistedCourt(name, dateCreated, dateUpdated, location, specialNotes, gym, courtStyle);
        court.setId(id);
        return court;
    }

    public static void assertValid(Court court) throws ValidationException {
        DomainObjectHelper.assertValidId(court.getId());
        assertNewValid(court);
    }

    public static void assertNewValid(Court court) {
        NamedDomainObjectHelper.assertValidName(court.getName());
        assertHasGym(court);
        assertHasCourtStyle(court);
    }

    private static void assertHasCourtStyle(Court court) {
        if(court.getCourtStyle() == null) {
            throw new ValidationException(ErrorConstants.COURT_NEEDS_A_COURT_STYLE_ERROR);
        }
        CourtStylesHelper.assertValid(court.getCourtStyle());
    }

    private static void assertHasGym(Court court) {
        if(court.getGym() == null) {
            throw new ValidationException(ErrorConstants.COURT_NEEDS_A_GYM_ERROR);
        }
        GymsHelper.assertValid(court.getGym());
    }


}
